package com.starsea.book.ui.newpage.utils

import java.text.DecimalFormat
import java.text.NumberFormat
import java.util.Locale

/**
 * @author Lee
 * @createTime 2021-05-12 17 GMT+8
 * @desc :
 */

/**
 * 格式化为金钱逗号格式  例如  100000  格式化为 1，0000
 */
fun Double.doubleSpString(): String =
  NumberFormat.getNumberInstance(Locale.US).format(this)

/**
 * 保留两位
 */
fun Double.doubleToDouble(): Double =
  DecimalFormat("#####################0.00").format(this).toDouble()

fun Double.doubleToDoubleStr(): String =
  DecimalFormat("#####################0.00").format(this)

fun Double.doubleToDoubleMoneyStr(): String =
  DecimalFormat("###,###,###,###,###,###,###0.00").format(this)

fun Double.doubleToOneStr(): String =
  DecimalFormat("#####################0.0").format(this)

fun Double.doubleToZeroStr(): String =
  DecimalFormat("#####################0").format(this)

/**
 * 保留四位
 */
fun Double.doubleToFour(): Double =
  DecimalFormat("#####################0.0000").format(this).toDouble()

fun Double.doubleToFourStr(): String =
  DecimalFormat("#####################0.0000").format(this)


/**
 * 两位去末尾0
 */
fun Double.finialDouble(): String = this.doubleToDouble().killing()

/**
 * Long 型转价格去掉末尾的0
 */
fun Long.finialLong(): String = (this / 100.0).doubleToDouble().killing()


/**
 * Long 型转价格 末尾保留两位
 */
fun Long.finialLongToDoubleZero(): String = (this / 100.0).doubleToDoubleStr()
fun Long.finialLongToDoubleZeroSort(): String = (this / 100.0).doubleSpString()
fun Long.finialLongToDoubleTwoZeroSort(): String = (this / 100.0).doubleToDoubleMoneyStr()

/**
 * Long 型转价格 末尾保留两位
 */
fun Long.longToDoubleZero(): Double = (this / 100.0).doubleToDouble()


/**
 * 四位去末尾0
 */
fun Double.finialFourDouble(): String = this.doubleToFour().killing()

/**
 * 去掉末尾0
 */
fun Double.killing(): String {
  val b = this.toInt()
  return if (this == b.toDouble()) {
    b.toString()
  } else {
    val num = this.toString()
    if (num.endsWith("0")) {
      num.substring(0, num.length - 1)
    } else {
      this.toString()
    }
  }
}
